import { addon, Permission } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import { AppServiceUtility } from 'src/projects/app/appService';
import { isPermission } from "src/projects/app/permission";
import { table_param } from "src/projects/app/util-tool";
import { ROUTE_PATH } from 'src/projects/router';
import { request } from "src/business/util_tool";
import { Select } from 'antd';

let { Option } = Select;
/**
 * 组件：新闻管理列表状态
 */
export interface NewsListViewState extends ReactViewState {
    // 作者列表
    author_list?: [];
    /** 接口名 */
    request_url?: string;
    // 带参
    select_type?: any;

}

/**
 * 组件：新闻管理列表
 * 描述
 */
export class NewsListView extends ReactView<NewsListViewControl, NewsListViewState> {
    private columns_data_source = [{
        title: '新闻标题',
        dataIndex: 'title',
        key: 'ntitle',
    }, {
        title: '新闻发布者',
        dataIndex: 'promulgator',
        key: 'promulgator',
    }, {
        title: '新闻描述',
        dataIndex: 'description',
        key: 'description',
    }, {
        title: '新闻发布时间',
        dataIndex: 'create_date',
        key: 'create_date',
    }, {
        title: '新闻状态',
        dataIndex: 'status',
        key: 'status',
        render: (text: string, record: any) => {
            if (text === 'audit_success') {
                return '已审核';
            } else if (text === 'audit_fail') {
                return '未通过';
            } else if (text === 'auditing') {
                return '审核中';
            } else {
                return text;
            }
        }
    }, {
        title: '新闻来源',
        dataIndex: 'source',
        key: 'source',
    }, {
        title: '显示状态',
        dataIndex: 'showing_status',
        key: 'showing_status',
    }];
    constructor(props: any) {
        super(props);
        this.state = {
            author_list: [],
            request_url: '',
            select_type: this.props.select_param && this.props.select_param['type'] ? this.props.select_param['type'] : '',
        };
    }

    /** 新增按钮 */
    add = () => {
        this.props.history!.push(ROUTE_PATH.changeNews);
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        const { select_type } = this.state;
        if ('icon_edit' === type) {
            // 来自活动所有列表
            if (select_type === 'sh') {
                this.props.history!.push(ROUTE_PATH.viewNews + '/' + contents.id);
            } else {
                this.props.history!.push(ROUTE_PATH.changeNews + '/' + contents.id + (contents.hasOwnProperty('step_no') && contents['step_no'] === 1 ? '/change' : ''));
            }
        }
    }

    componentDidMount() {
        request(this, AppServiceUtility.person_org_manage_service.get_personnel_worker!({}))
            .then((datas: any) => {
                console.info(datas);
                this.setState({
                    author_list: datas.result,
                });
            });

    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
        });
    }
    render() {
        const redeirect = isPermission(this.props.permission!);
        if (redeirect) {
            return redeirect;
        }

        let author = this.state.author_list;
        let author_list: any[] = [];
        author!.map((item, idx) => {
            author_list.push(<Option key={item['id']}>{item['name']}</Option>);
        });

        let select_param = this.props.select_param;
        let param = {};
        if (select_param.hasOwnProperty('type')) {
            param['all'] = select_param['type'];
        }

        let news_data = {
            type_show: false,
            edit_form_items_props: [
                {
                    type: InputType.input,
                    label: "新闻标题",
                    decorator_id: "title"
                }, {
                    type: InputType.input,
                    label: "新闻发布者",
                    decorator_id: "promulgator",
                    option: {
                        placeholder: "请输入新闻发布者",
                        // childrens: author_list
                    }
                }, {
                    type: InputType.rangePicker,
                    label: "发布时间",
                    decorator_id: "date_range"
                },
                // {
                //     type: InputType.radioGroup,
                //     label: "审核状态",
                //     decorator_id: "status",
                //     option: {
                //         options: [
                //             {
                //                 label: '已审核',
                //                 value: 'audit_success'
                //             }, {
                //                 label: '未通过',
                //                 value: 'audit_fail'
                //             }, {
                //                 label: '审核中',
                //                 value: 'auditing'
                //             },
                //         ]
                //     }
                // },
            ],
            btn_props: [{
                label: '新增新闻',
                btn_method: this.add,
                icon: 'plus'
            }],
            // on_click_del: this.on_click_del,
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.article_service,
            service_option: {
                select: {
                    service_func: this.state.request_url,
                    service_condition: [param, 1, 10]
                },
                delete: {
                    service_func: 'delete_news'
                }
            },
            searchExtraParam: param,
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.select_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let news_list = Object.assign(news_data, table_param);
        return (
            <SignFrameLayout {...news_list} />
        );
    }
}

/**
 * 控件：新闻管理列表
 * 描述
 */
@addon('NewsListView', '新闻管理列表', '描述')
@reactControl(NewsListView, true)
export class NewsListViewControl extends ReactViewControl {
    /** 视图权限 */
    public permission?: Permission;
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
    select_param?: any = {};
}